#include"stdlib.h"
#define M 5
typedef struct node {int n[2];
	 struct node *link;}JD;
int a[M][M],n;
void makerank()
	 {void put();
	 int i,j;
     for(i=0;i<n;i++)
	for(j=0;j<n;j++)
		 a[i][j]=random(2);
	  a[0][0]=1;
	  a[n][n]=0;
	  put();
	  getch();
	  return;}
JD *findpath(JD *top)
	 {void put();
	 int x,y,i,j;
     JD *p;
	  if(top==NULL) return NULL;
	  i=top->n[0];
     j=top->n[1];
	  for(x=i+1;x>=i-1;x--)
		for(y=j+1;y>=j-1;y--)
		if(x>=0&&x<n&&y>=0&&y<n)
		 if(a[x][y]==0)
			 {a[x][y]=8;
			  p=(JD *)malloc(sizeof(JD));
		     p->n[0]=x;
		     p->n[1]=y;
		     p->link=top;
			  top=p;   put();
			  if((x==(n-1))&&(y==(n-1)))
			  return top;
		     findpath(top);}
		x=top->n[0];
		y=top->n[1];
		a[x][y]=1;
		p=top;
		top=p->link;
		free(p);
		if(top==NULL)
		return top;

		findpath(top);
     }
void put()
  {int i,j;
   for(i=0;i<n;i++)
      {printf("\n");
       for(j=0;j<n;j++)
	  printf("%d  ",a[i][j]);
		 printf("\n");}
	printf("\n");
   }
main()
   {int i,j;
    JD *top,*p,*h;
    printf("input the rank number:");
    scanf("%d",&n);
    for(i=0;i<M;i++)
	for(j=0;j<M;j++)
	     a[i][j]=10;
    top=(JD *)malloc(sizeof(JD));
    top->n[0]=0;
    top->n[1]=0;
    top->link=NULL;
	 do {makerank();
		  top=findpath(top);
		 } while(top==NULL);

	  put();getch();
     h=(JD *)malloc(sizeof(JD));
     h->link=NULL;
     while(top!=NULL)
	 { i=top->n[0];
	   j=top->n[1];
	   a[i][j]=8;
	   p=top;
	   top=top->link;
	   p->link=h->link;
	   h->link=p;}
	   top=h->link;
	   free(h);
    printf("the path described by map('8' is the feasible place)");
    put();
    printf("the order describe by word is :\n");
    while(top!=NULL)
    printf("a[%d][%d]-->",top->n[0],top->n[1]);
    printf("press any key to quit!");
    getch();
  }